<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="能源微基站名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入能源微基站名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="告警类型" prop="warningType">
        <el-select v-model="queryParams.warningType" placeholder="请选择告警类型" clearable>
          <el-option v-for="dict in dict.type.warn_types" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="告警等级" prop="warningLevel">
        <el-select v-model="queryParams.warningLevel" placeholder="请选择告警等级" clearable>
          <el-option v-for="dict in dict.type.warn_level" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="工单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择工单状态" clearable>
          <el-option v-for="dict in dict.type.order_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:info:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:info:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="能源微基站名称" align="center" prop="name" />
      <el-table-column label="mac地址" align="center" prop="mac" />
      <el-table-column label="告警类型" align="center" prop="warningType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.warn_types" :value="scope.row.warningType" />
        </template>
      </el-table-column>
      <el-table-column label="设备类型" align="center" prop="deviceType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.device_type" :value="scope.row.deviceType" />
        </template>
      </el-table-column>
      <el-table-column label="告警等级" align="center" prop="warningLevel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.warn_level" :value="scope.row.warningLevel" />
        </template>
      </el-table-column>
      <el-table-column label="处理人" align="center" prop="userId" />
      <el-table-column label="工单状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="
            (userName == 'admin' || scope.row.createBy == userName) &&
            scope.row.clzt != '2'
          " size="mini" type="text" icon="el-icon-edit" @click="handleZf(scope.row)"
            v-hasPermi="['system:lccl:edit']">转发工单</el-button>
          <el-button v-if="
            (userName == 'admin' || scope.row.createBy == userName) &&
            scope.row.clzt !== '2'
          " size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:workOrder:edit']">处理工单</el-button>
          <el-button v-if="
            (userName == 'admin' || scope.row.createBy == userName) &&
            scope.row.clzt !== '2'
          " size="mini" type="text" icon="el-icon-tickets" @click="getCk(scope.row)"
            v-hasPermi="['system:workOrder:edit']">误报</el-button>

          <el-button size="mini" type="text" icon="el-icon-tickets" @click="check(scope.row)"
            v-hasPermi="['system:workOrder:edit']">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改工单信息对话框 -->
    <!-- 添加或修改派工单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="接受单位">
          <el-input v-model="jsdwmc" placeholder="请输入接受单位" :disabled="true">
          </el-input>
        </el-form-item>
        <el-form-item label="处理时限" prop="clsx">
          <el-date-picker clearable v-model="form.clsx" type="date" value-format="yyyy-MM-dd" :disabled="true"
            placeholder="请选择处理时限">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="下达单位">
          <el-input placeholder="请输入下达单位" :disabled="true" v-model="xddwmc">
          </el-input>
        </el-form-item>
        <el-form-item label="下达时间" prop="xdsj">
          <el-date-picker clearable v-model="form.xdsj" type="date" value-format="yyyy-MM-dd" :disabled="true"
            placeholder="请选择下达时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="下达人" prop="xdr">
          <el-input v-model="form.xdr" placeholder="请输入下达人" :disabled="true" />
        </el-form-item>
        <el-form-item label="处理需求" prop="clxq">
          <el-input v-model="form.clxq" :disabled="true" />
        </el-form-item>
        <el-form-item label="处置信息" prop="czxx">
          <el-input v-model="form.czxx" placeholder="请输入处置信息" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <gdckLogM ref="gdckLogM"></gdckLogM>
    <gdzflog ref="gdzflog" title="工单转发"></gdzflog>
    <gdWbLogM ref="gdWbLogM"></gdWbLogM>

  </div>
</template>

<script>
import {
  exportNavigationMark,
  listWorkOrder,
  getWorkOrder,
  delWorkOrder,
  addWorkOrder,
  updateWorkOrder,
} from "@/api/system/workOrder";
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/system/gd/gd"
import { selectMaterial, selectPlanList } from "@/api/system/Plan";
import gdckLogM from "@/components/gdLog/gdckLogM.vue";
import { deptTree } from "@/api/daping/daping";
import gdzflog from "@/components/gdLog/gdzflog.vue";
import gdWbLogM from "@/components/gdLog/gdWbLogM.vue";
export default {
  name: "Info",
  components: { gdckLogM, gdzflog, gdWbLogM },
  dicts: ['warn_level', 'warn_types', 'order_status', 'device_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工单信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mac: null,
        warningInfoId: null,
        userId: null,
        status: null,
      },
      // 表单参数
      form: { clsx: "" },
      // 表单校验
      rules: {
      },
      userName: this.$store.state.user.name,
      materialList: [],
      optionMData: [],
      optionYaData: [],
      ckTableData: [],
      jsdwmc: null,
      xddwmc: null, pfNum: null,
    }
  },
  created() {
    this.userName = this.$store.state.user.name;
    this.getList()
    this.getDeptTree();
  },
  methods: {
    /** 查询工单信息列表 */
    getList() {
      this.loading = true
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    optionData(arr, result = []) {
      arr.forEach((item) => {
        result.push({ label: item.label, value: item.id });
        if (item.children && item.children.length !== 0) {
          this.optionData(item.children, result);
        }
      });
      return JSON.parse(JSON.stringify(result));
    },
    deptData(dept, result = "") {
      this.optionMData.forEach((item) => {
        if (item.value == dept) {
          result = item.label;
        }
      });
      return result;
    },
    getDeptTree() {
      deptTree().then((res) => {
        if (res.code == 200) {
          this.treeOp = res.data;
          this.optionMData = this.optionData(this.treeOp);
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        mac: null,
        warningInfoId: null,
        userId: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm("form")
    },
    handleZf(item) {
      item.yjid = item.id;
      this.$refs.gdzflog.getInit(JSON.parse(JSON.stringify(item)));
      this.$refs.gdzflog.open = true;
    },
    async getCk(item) {
      this.$refs.gdWbLogM.getCk(item);
    },
    check(row) {
      this.$refs.gdckLogM.getCk(row.yjid);
    },
    handleUpdate(row) {
      this.reset();
      const id = row.yjid;
      this.queryParams2.workId = id;
      getWorkOrder(id).then((response) => {
        if (response.code == 200) {

          this.form = response.data;
          this.form.uavDataId = row.uavDataId;
          this.form.yjlx = row.yjlx;
          this.open = true;
          this.title = "处理工单";
          console.log("dfadsfadsfasdfsaf");
          this.xddwmc = this.deptData(response.data.xddw);
          this.jsdwmc = this.deptData(response.data.jsdw);
        }
      });
      const yjlxList = ["6", "7", "8", "9"];
      if (yjlxList.includes(row.yjlx)) {
        this.pfNum = "ck";
      } else {
        this.pfNum = "";
      }
      this.queryParams1.yjlx = row.yjlx;
      selectPlanList(this.queryParams1).then((response) => {
        console.log("=======" + response);
        this.optionYaData = response.data;
      });
    },
    handleYaChange(selectedId) {
      // 如果需要，还可以根据ID查找对应的预案
      console.log(this.optionYaData);
      const selecteYa = this.optionYaData.find(
        (item) => item.id === selectedId
      );
      console.log(selecteYa);
      if (selecteYa) {
        this.form.yanr = selecteYa.yanr;
      }
      this.queryParams2.planId = selectedId;
      selectMaterial(this.queryParams2).then((response) => {
        console.log("=======" + response);
        this.materialList = response.data;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加工单信息"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.yjid
      getInfo(id).then(response => {
          if (response.code == 200) {
          this.form = response.data;
          this.form.uavDataId = row.uavDataId;
          this.form.yjlx = row.yjlx;
          this.open = true;
          this.title = "处理工单";
          console.log("dfadsfadsfasdfsaf");
          this.xddwmc = this.deptData(response.data.xddw);
          this.jsdwmc = this.deptData(response.data.jsdw);
        }
      })

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除工单信息编号为"' + ids + '"的数据项？').then(function () {
        return delInfo(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => { })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
